package com.base.myproject.client.init;

import com.base.myproject.client.busi.ClientEnv.SystemInfo;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.OpenNewOnStartUp;
import com.base.myproject.client.tools.BaseStoreCommand;
import com.base.myproject.client.tools.CommandFactory;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.BaseTabItem;
import com.base.myproject.client.ui.BaseTabPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.ProgressBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class StartUp {
	
	 
	 public StartUp()
	 {
		 init();
	 }
	 void init(){
		 
	 }
	 public void start()
	 {
		
//		 final Timer t = new Timer() {  
//			 float i;  
//			 @Override  
//	          public void run() {  
//	            bar.updateProgress(i / 100, (int) i + "% Complete");  
//	            i += 5;  
//	            box.setMessage("2"+i);
//	            if (i > 105) {  
//	              cancel();  
//	              box.close();  
//	            //  Info.display("Message", "Items were loaded", "");  
//	            }  
//	          }  
//		 };
//		 t.scheduleRepeating(500);  
//		 float i=1;  
//		 int total = 2;
//
//		 bar.updateProgress(i++ / total, (int) i/total*100 + "% Complete");  
//		 StoreCache.getInstance().getCcodestore(null);
//
//
//		 bar.updateProgress(i++ / total, (int) i/total*100  + "% Complete");  
//		 StoreCache.getInstance().getBcodestore(null);
//		 Loading.getinstance().loading();
//		 final Timer t = new Timer() {  
//		 float i;  
//		 @Override  
//          public void run() {  
//           Debug.printf("run...");
//          
//          }  
//	 };
//	 t.scheduleRepeating(3000);  
			GWT.runAsync(new RunAsyncCallback(){
		         public void onFailure(Throwable err){
		            Window.alert("代码下载错误，错误可能源自网络链接已经断了。");
		         }
		 
		         public void onSuccess(){
		        	 loadSysteminfo();
		    		 loadCache();
		    		 loadSetting();
		         }
		      });
		
//		 
//		 final Timer t = new Timer() {  
//			 float i;  
//			 @Override  
//	         public void run() {  
//	         
//	           i += 5;  
//	           
//	           if (i > 105) {  
//	        	   cancel();  
//	        	   box.close();  
//	           }  
//	         }  
//		 };
//		 t.scheduleRepeating(500);  
		
	 }
	 private void loadSetting() {
		
		 final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
		 greetingService.getDataSet("select name ,settingb from systemsetting where name ='opennew' ", new AsyncCallback<DataSet>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(DataSet result) {
				if(result.getRowCount()>0)
				{
					if(result.getValue(0, "settingb").equals("1")){
						BaseTabItem ti = new BaseTabItem();
						ti.setClosable(false);
						OpenNewOnStartUp st=new OpenNewOnStartUp();
						st.complete();
						ti.add(st);
						ti.setText("工作台");
						BaseTabPanel.getInstance().add(ti);
					}
				}
				
			}
			 
			 
		 });
	}
	int total  = 9;//总数
	  int comple=0; //完成数，
	 /**
	  * 载入信息，需要就加入，软後设置在如总数
	  */
	 private void loadSysteminfo()
	 {
		 
		
		 final MessageBox box = MessageBox.progress("请等待...", "正在获取系统信息...",  
	     "载入中...");  
		final ProgressBar bar = box.getProgressBar();  
		
		 final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService.getDataSet("select * from systeminfo where exists (select * from bcode where bcode.systemid = systeminfo.id and bcode  = '"+Env.getEnvbcode()+"')",new AsyncCallback<DataSet>(){

				@Override
				public void onFailure(Throwable caught) {
					
					
				}

				@Override
				public void onSuccess(DataSet result) {
					Debug.printf(result.toString());
					 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
					 
					SystemInfo.getInstance().setId(result.getValue(0, "id")==null?"":result.getValue(0, "id"));
					SystemInfo.getInstance().setName(result.getValue(0, "name")==null?"":result.getValue(0, "name"));
					SystemInfo.getInstance().setPrintmessage(result.getValue(0, "printmessage")==null?"":result.getValue(0, "printmessage"));
					SystemInfo.getInstance().setTel(result.getValue(0, "tel")==null?"":result.getValue(0, "tel"));
					SystemInfo.getInstance().setTitle(result.getValue(0, "title")==null?"":result.getValue(0, "title"));
					SystemInfo.getInstance().setFax(result.getValue(0, "fax")==null?"":result.getValue(0, "fax"));
					SystemInfo.getInstance().setSlogan(result.getValue(0, "slogan")==null?"":result.getValue(0, "slogan"));
					
					//----最后一条要关闭
					 bar.updateProgress(100 / 100, (int) 100 + "% 完成");  
					 box.close();  
				}
				
			});
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.getBaseccodestore();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.getBaseMethodStore();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.getBaseSysteminfostore();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.loadBcode_800();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.loadBcode_801();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.loadBcode_802();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.loadBcode_900();
//			 bar.updateProgress((comple++) / total, (int) ((comple++) / total) + "% 完成");  
//			CodeNameMapFactory.loadBcode_810();
//			
			
	 }
	 void loadCache()
	 {
//			BaseStoreCommand bsc = new BaseStoreCommand("Base_Bcode","初始化人员....");
//			BaseStoreCommand bsc2 = new BaseStoreCommand("Base_Ccode","初始化会员....");
//			BaseStoreCommand bsc3 = new BaseStoreCommand("Base_s_service_po","初始化会员....");
//			BaseStoreCommand bsc4 = new BaseStoreCommand("Base_s_service_po1","初始化会员1....");
//			BaseStoreCommand bsc5 = new BaseStoreCommand("Base_s_service_po2","初始化会员2....");
//			BaseStoreCommand bsc6 = new BaseStoreCommand("Base_s_service_po3","初始化会员3....");
//			BaseStoreCommand bsc7 = new BaseStoreCommand("Base_s_service_po4","初始化会员4....");
//			BaseStoreCommand bsc8 = new BaseStoreCommand("Base_s_service_po5","初始化会员5....");
//			
//			 CommandFactory.addCommand(bsc);
//			 CommandFactory.addCommand(bsc2);
//			 CommandFactory.addCommand(bsc3);
//			 CommandFactory.addCommand(bsc4);
//			 CommandFactory.addCommand(bsc5);
//			 CommandFactory.addCommand(bsc6);
//			 CommandFactory.addCommand(bsc7);
//			 CommandFactory.addCommand(bsc8);
			final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
	greetingService
			.getDataSet("select name from sqltable where type=1", new AsyncCallback<DataSet>(){

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.alert("错误",
							"网络连接失败", null);
					
				}

				@Override
				public void onSuccess(DataSet result) {
					for(int i=0;i<result.getRowCount();i++)
					{
						String name =result.getValue(i, "name"); 
						String desc = result.getValue(i, "_desc"); 
						BaseStoreCommand bsc = new BaseStoreCommand(name,desc);
						CommandFactory.addCommand(bsc);
						
					}
					 final MessageBox box = MessageBox.wait("加载",  
					            "正在加载数据请等待...", "加载...");  
					 final Timer t = new Timer() {  
				         
				  
				          @Override  
				          public void run() {  
				           
				           
				            if (CommandFactory.getCommandSize() == 0) {  
				              cancel(); 
				              box.close();  
				              Info.display("完成	", "加载完成", "");  
				            }  
				          }  
				        };  
				        t.scheduleRepeating(500);  	
					
				}
				
			});
		
		 
			 
			
		        
	 }
}
